//**********************************************************
//    File Name: FrmChart.cs
//    Author: Team Motif
//    Date Created : 04/13/2007
//    Current Owner: Team Motif 
//                                                                            
//    Purpose: 
//    This form will be used for adding chart information for the clients.
//
//	  Copyright 2007
//**********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Library;

namespace Elderhealth
{
    public partial class FrmChart : Form
    {


        /// <summary>
        /// Default constructor.  Assumes new chart data
        /// </summary>
        public FrmChart(Client client, FrmViewClient callingFrm)
        {
            mCallingForm = callingFrm;
            mClient = client;
            mChartObject = new Chart();
            mCreatingNewChart = true;
            InitializeComponent();
            populateWidgets(mChartObject);
            this.mCreateButton.Visible = true;
            this.mSaveButton.Visible = false;
        }
        /// <summary>
        /// Overloaded constructor.  Assumes modifying existing chart
        /// </summary>
        /// <param name="chart">Chart object</param>
        public FrmChart(Chart chart, FrmViewClient callingFrm)
        {
            mCallingForm = callingFrm;
            mChartObject = chart;
            mClient = null;
            mCreatingNewChart = false;
            InitializeComponent();
            populateWidgets(mChartObject);
            this.mCreateButton.Visible = false;
            this.mSaveButton.Visible = true;

        }

        /// <summary>
        /// Populates the values of the widgets using a Chart object
        /// </summary>
        /// <param name="chart">Chart object</param>
        private void populateWidgets(Chart chart)
        {
            if (null == chart)
            {
                LOG.error("FrmChart::populateWidgets: received null chart");
                return;
            }
            this.mChart.Text = chart.GetChartData();
            if (mCreatingNewChart)
            {
                this.mAuthorLbl.Text = "Created By";
            }
            else
            {
                this.mAuthorLbl.Text = "Modified By";
                this.mChart.Text = chart.GetChartData();
            }

            this.mAuthor.Text = chart.GetModifyAuthor();

            DateTime dt = DateTime.UtcNow;
            this.mDate.Text = dt.ToLongDateString() + " : " + dt.ToLongTimeString();
        }


        /// <summary>
        /// Creates a new chart
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mCreateButton_Click(object sender, EventArgs e)
        {
            if (mCreatingNewChart)
            {
                if (null == mClient)
                {
                    LOG.error("FrmChart::mCreateButton_Click: mClient is null");
                    return;
                }
                    
                mClient.AddChart(RetrieveChartDataFromForm());
                if (mCallingForm != null)
                {
                    mCallingForm.UpdateClientChartView();
                }
                this.Close();
            }
        }

        /// <summary>
        /// Pulls data from form widgets and populates a Chart object
        /// </summary>
        /// <returns>Chart</returns>
        private Chart RetrieveChartDataFromForm()
        {
            Chart chart = new Chart(this.mAuthor.Text.ToString());
            chart.SetChartData(this.mChart.Text.ToString(), this.mAuthor.Text.ToString());
            return chart;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mSaveButton_Click(object sender, EventArgs e)
        {
            if (!mCreatingNewChart)
            {
                mChartObject.SetChartData(mChart.Text.ToString(), mAuthor.Text.ToString());
                if (null == mCallingForm)
                {
                    LOG.error("FrmChart::mSaveButton_Click: mClassingForm is null");
                    return;
                }
                    
                mCallingForm.UpdateClientChartView();
                this.Close();
            }
        }

        /// <summary>
        /// Reverts chart data to previous states
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mCancelButton_Click(object sender, EventArgs e)
        {
            // revert text to previous states
            mChart.Text = mChartObject.GetChartData();
        }

        /// <summary>
        /// Trigger events when text changes in chart area
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mChart_TextChanged(object sender, EventArgs e)
        {
            mCancelButton.Enabled = true;
            mCreateButton.Enabled = true;
            mSaveButton.Enabled = true;

        }

        /// <summary>
        /// trigger events when author text changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mAuthor_TextChanged(object sender, EventArgs e)
        {
            mCancelButton.Enabled = true;
            mCreateButton.Enabled = true;
            mSaveButton.Enabled = true;
        }


        // private data region
        private bool mCreatingNewChart;
        private Client mClient;
        private Chart mChartObject;
        private FrmViewClient mCallingForm;


    }
}